
import 'dart:async';
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({Key? key});

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> with SingleTickerProviderStateMixin,WidgetsBindingObserver {

  final flutterLogo = Image.asset("images/splash.gif", fit: BoxFit.cover);
  late Timer timeout;

  @override
  void initState() {
    super.initState();
    timeout = scheduleTimeout(3 * 1000);
  }

  Timer scheduleTimeout([int milliseconds = 10000]) => Timer(Duration(milliseconds: milliseconds), handleTimeout);

  void handleTimeout() {
    Navigator.pop(context);
  }

  @override
  Widget build(BuildContext context) {
    return ConstrainedBox(
        constraints: BoxConstraints.expand(),
        child: Stack(
            alignment:Alignment.center , //指定未定位或部分定位widget的对齐方式
            children: <Widget>[
              Container(
                color: Colors.white,
              ),
              Positioned(
                top: 200,
                child: flutterLogo,
              ),
              Positioned(
                  bottom: 200,
                  child: GestureDetector(
                      onTap: ()=>{
                        timeout.cancel(),
                        handleTimeout()
                      },
                      child: CustomPaint(
                          painter: Sky(),
                          child:Padding(
                              padding: const EdgeInsets.only(right: 15,left: 5,top: 2,bottom: 2),
                              child:SizedBox(
                                  width: 100.0,
                                  height: 30.0,
                                  child: Center(
                                    child: Text(
                                        "立即进入",
                                        style: TextStyle(
                                          fontSize: 18.0,
                                          color: Colors.blueGrey,
                                          height: 1.2,
                                          fontFamily: "Courier",
                                          decoration:TextDecoration.none,
                                        )
                                    ),
                                  )
                              )
                          )
                      )
                  )
              )
            ]
        )
    );
  }
}

class Sky extends CustomPainter {
  double degree = pi / 180;
  @override
  void paint(Canvas canvas, Size size) {

    var paint = Paint();
    paint.color = Colors.blueGrey;
    paint.style = PaintingStyle.stroke;
    paint.strokeJoin = StrokeJoin.round;
    paint.strokeWidth = 2;

    var path = Path();
    var oval1 = Rect.fromLTRB(0, 0, 12, 12);
    var oval2 = Rect.fromLTRB(0, size.height - 12, 12, size.height);
    path.addArc(oval2, degree*90, degree*90);
    path.lineTo(0, 6);
    path.addArc(oval1, degree*180, degree*90);
    path.lineTo(size.width - size.width/6, 0);
    path.lineTo(size.width, size.height/2);
    path.lineTo(size.width - size.width/6, size.height);
    path.lineTo(6, size.height);

    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(Sky oldDelegate) => false;

  @override
  bool shouldRebuildSemantics(Sky oldDelegate) => false;
}



